package values.item
{
import values.avatar.IAvatarValue;
import values.avatar.IPropertyProvider;
import values.events.ItemValueEvent;

[Event(name="upgradeSuccess", type="values.events.ItemValueEvent")]
[Event(name="upgradeFail", type="values.events.ItemValueEvent")]
/**
 * EquipmentItemValue
 * @author jliang
 *
 */
public class EquipmentItemValue extends ItemValue implements IUpgradebleItemValue,  IPropertyProvider
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function EquipmentItemValue()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get equipmentTemplate():EquipmentItemTemplateValue
	{
		return EquipmentItemTemplateValue(template);
	}	
	
	protected var _owner:IAvatarValue;

	public function get owner():IAvatarValue
	{
		return _owner;
	}

	public function set owner(value:IAvatarValue):void
	{
		_owner = value;
	}
	//==========================================================================
	//  Overridden methods: ItemValue
	//==========================================================================		
	override public function initialize(params:*):void
	{
		super.initialize(params);
		
		if (!Object(params).hasOwnProperty("level"))
		{
			setLevel(1);
		}
	}
	
	override public function equals(value:IItemValue):Boolean
	{
		if (key != value.key) return false;
		
		if (level != IUpgradebleItemValue(value).level) return false;
		
		return true;
	}
	
	override public function toObject():Object
	{
		var obj:Object = super.toObject(); 
		obj.push({
			level: level
		});
		return obj;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function isMatchParams(obj:Object):Boolean
	{
		return level == obj.level;
	}
	//==========================================================================
	//  interface - IUpgradebleItemValue
	//==========================================================================	
	protected function setLevel(value:int):void
	{
		params.level = value;
	}
	
	public function get level():int
	{
		return params.level;
	}
	
	public function get currentUpgradeValue():ItemUpgradeValue
	{
		return equipmentTemplate.getUpgradeByLevel(level);
	}
	
	public function setUpgradeCondition(...args):Boolean
	{
		var result:int = checkUpgradeCondition.apply(this, args);
		var evt:ItemValueEvent;
		if (result > 0)
		{
			evt = new ItemValueEvent(ItemValueEvent.UPGRADED_SUCCESS);
			setLevel(level + result);
		}
		else
		{
			evt = new ItemValueEvent(ItemValueEvent.UPGRADED_FAIL);
		}		
		evt.params = this;
		this.dispatchEvent(evt);
        
        return result > 0;
	}
	
	protected function checkUpgradeCondition(...args):int
	{
		return 1;
	}
	
	/**
	*  装备部位
	* @return 
	* 
	*/	
	public function get position():String
	{
		return equipmentTemplate.position;
	}
	
	/**
	 *  装备类型
	 * @return 
	 * 
	 */	
	public function get equipType():String
	{
		return equipmentTemplate.equipType;
	}
	
	/**
	 *  生命值
	 * @return 
	 * 
	 */	
	[Prop(mapping="maxHP")]
	public function get maxHP():Number
	{
		return Number(currentUpgradeValue.effect.max_hp);
	}
	
	/**
	 *  能量值
	 * @return 
	 * 
	 */	
	[Prop(mapping="initMP")]
	public function get initMP():Number
	{
		return Number(currentUpgradeValue.effect.max_mp);
	}
	
	/**
	 *  能量恢复效率
	 * @return 
	 * 
	 */	
	[Prop(mapping="mpRecover")]
	public function get mpRecover():Number
	{
		return Number(currentUpgradeValue.effect.mp_recover);
	}
	
	/**
	 *  战斗
	 * @return 
	 * 
	 */	
	[Prop(mapping="normalPower")]
	public function get normalPower():Number
	{
		return Number(currentUpgradeValue.effect.normal_power);
	}
	
	/**
	 *  特技
	 * @return 
	 * 
	 */	
	[Prop(mapping="skillPower")]
	public function get skillPower():Number
	{
		return Number(currentUpgradeValue.effect.skill_power);
	}
	
	/**
	 *  元素
	 * @return 
	 * 
	 */	
	[Prop(mapping="elementPower")]
	public function get elementPower():Number
	{
		return Number(currentUpgradeValue.effect.element_power);
	}
	
	/**
	 *  物理防御
	 * @return 
	 * 
	 */	
	[Prop(mapping="normalDefense")]
	public function get normalDefense():Number
	{
		return Number(currentUpgradeValue.effect.normal_defense);
	}
	
	/**
	 *  技能防御
	 * @return 
	 * 
	 */	
	[Prop(mapping="skillDefense")]
	public function get skillDefense():Number
	{
		return Number(currentUpgradeValue.effect.skill_defense);
	}
	
	/**
	 *  元素防御
	 * @return 
	 * 
	 */	
	[Prop(mapping="elementDefense")]
	public function get elementDefense():Number
	{
		return Number(currentUpgradeValue.effect.element_defense);
	}
	
	/**
	 *  普通攻击力
	 * @return 
	 * 
	 */	
	[Prop(mapping="normalDamage")]
	public function get normalDamage():Number
	{
		return Number(currentUpgradeValue.effect.normalDamage);
	}
	
	/**
	 *  特技攻击力
	 * @return 
	 * 
	 */	
	[Prop(mapping="skillDamage")]
	public function get skillDamage():Number
	{
		return Number(currentUpgradeValue.effect.skill_damage);
	}
	
	/**
	 *  元素攻击力
	 * @return 
	 * 
	 */	
	[Prop(mapping="elementDamage")]
	public function get elementDamage():Number
	{
		return Number(currentUpgradeValue.effect.element_damage);
	}
	
	/**
	 *  移动力
	 * @return 
	 * 
	 */	
	[Prop(mapping="moveCapability")]
	public function get moveCapability():Number
	{
		return Number(currentUpgradeValue.effect.move_capability);
	}
	
	/**
	 *  命中等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="hitRate")]
	public function get hitRate():Number
	{
		return Number(currentUpgradeValue.effect.hit_rate);
	}
	
	/**
	 *  闪避等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="dodgeRate")]
	public function get dodgeRate():Number
	{
		return Number(currentUpgradeValue.effect.dodge_rate);
	}
	
	/**
	 *  穿透等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="penetrateRate")]
	public function get penetrateRate():Number
	{
		return Number(currentUpgradeValue.effect.penetrate_rate);
	}
	
	/**
	 *  抵抗等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="resistRate")]
	public function get resistRate():Number
	{
		return Number(currentUpgradeValue.effect.resist_rate);
	}
	
	/**
	 *  强攻等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="enforceRate")]
	public function get enforceRate():Number
	{
		return Number(currentUpgradeValue.effect.enforce_rate);
	}
	
	/**
	 *  抵挡等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="blockRate")]
	public function get blockRate():Number
	{
		return Number(currentUpgradeValue.effect.block_rate);
	}
	
	/**
	 *  抵挡力
	 * @return 
	 * 
	 */		
	[Prop(mapping="blockEffect")]
	public function get blockEffect():Number
	{
		return Number(currentUpgradeValue.effect.block_effect);
	}
	
	/**
	 *  强韧等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="toughness")]
	public function get toughness():Number
	{
		return Number(currentUpgradeValue.effect.toughness);
	}
	
	/**
	 *  暴击等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="critRate")]
	public function get critRate():Number
	{
		return Number(currentUpgradeValue.effect.crit_rate);
	}
	
	/**
	 *  暴击伤害加成
	 * @return 
	 * 
	 */		
	[Prop(mapping="critEffect")]
	public function get critEffect():Number
	{
		return Number(currentUpgradeValue.effect.crit_effect);
	}
	
	/**
	 *  御体等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="defenseRate")]
	public function get defenseRate():Number
	{
		return Number(currentUpgradeValue.effect.defense_rate);
	}
	
	/**
	 *  压制等级
	 * @return 
	 * 
	 */		
	[Prop(mapping="suppressRate")]
	public function get suppressRate():Number
	{
		return Number(currentUpgradeValue.effect.suppress_rate);
	}
	
	/**
	 *  压制力
	 * @return 
	 * 
	 */	
	[Prop(mapping="suppressEffect")]
	public function get suppressEffect():Number
	{
		return Number(currentUpgradeValue.effect.suppress_effect);
	}
	
	/**
	 *  先攻值
	 * @return 
	 * 
	 */		
	[Prop(mapping="actRate")]
	public function get actRate():Number
	{
		return Number(currentUpgradeValue.effect.act_rate);
	}
}
}





